package com.wtb.pub;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.wtb.domain.DataCache;
import com.wtb.qq.domain.QqDetailInfo;
import com.wtb.qq.domain.QqOauth;
import com.wtb.rr.domain.RrDetailInfo;
import com.wtb.rr.domain.RrOauth;
import com.wtb.utils.DateUtils;

@SuppressWarnings("serial")
public class OpenAndClose extends WtbBaseServlet {
	private static final Log log = LogFactory.getLog(OpenAndClose.class);
	protected RrOauth rrOauth;
	protected QqOauth qqOauth;
	protected RrDetailInfo rrDetailInfo;
	protected QqDetailInfo qqDetailInfo;
	protected String status;
	protected static final String OPEN = "1";
	protected static final String CLOSE = "0";
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		super.doPost(request, response);
		if (lastMap == null ||lastMap.size() != 2) {
			response.sendRedirect("error?err_code=1003");
			return;
		}
		rrOauth = (RrOauth) lastMap.get("rr");
		qqOauth = (QqOauth) lastMap.get("qq");
		rrDetailInfo = (RrDetailInfo) lastMap.get("rr");
		qqDetailInfo = (QqDetailInfo) lastMap.get("qq");
		status = request.getParameter("status");
	}
	
	/**
	 * 关闭同步
	 * @return 成功 true  失败 fasle
	 * @throws IOException
	 */
	protected boolean closeTb() throws IOException{
		try {
			qqDao.del(qqOauth);
			rrDao.del(rrOauth);
			DataCache.synRemQqMap(qqOauth.getUni());
			DataCache.synRemRrMap(rrOauth.getUni());
			DataCache.value.decrementAndGet();
		} catch (Exception e) {
			log.error("关闭同步错误：", e);
			return false;
		}
		//清空session
		session.setAttribute("lastMap", null);
		session.setAttribute("r2q", null);
		session.setAttribute("q2r", null);
		//设置状态
		session.setAttribute("status", "closed");
		return true;
	}
	
	/**
	 * 开启同步
	 * @param rrstatus 人人到微博状态 1开启 0关闭
	 * @param qqstatus 微博到人人状态 1开启 0关闭
	 * @return 成功 true  失败 false
	 * @throws IOException
	 */
	protected boolean openTb(String rrstatus , String qqstatus) throws IOException{
		rrDetailInfo.setRef(qqOauth.getUni());
		qqDetailInfo.setRef(rrOauth.getUni());
		rrDetailInfo.setR2q(rrstatus);
		qqDetailInfo.setQ2r(qqstatus);
		rrDetailInfo.setR2s("0");
		qqDetailInfo.setQ2s("0");
		//  在map中加入oauth 插入数据库
		try {
			qqDao.insert(qqDetailInfo);
			rrDao.insert(rrDetailInfo);
			DataCache.rrlastTime.putIfAbsent(rrOauth.getUni(),DateUtils.createTime());
			DataCache.qqlastTime.putIfAbsent(qqOauth.getUni(),DateUtils.systemTime());
			DataCache.synPutRrMap(rrOauth);
			DataCache.synPutQqMap(qqOauth);
			DataCache.increment();
			if (rrstatus.equals(OPEN)) {
				session.setAttribute("r2q", "close");
			}
			if (qqstatus.equals(OPEN)) {
				session.setAttribute("q2r", "close");
			}
		} catch (Exception e) {
			//失败后还原数据
			rrDetailInfo.setRef(null);
			qqDetailInfo.setRef(null);
			rrDetailInfo.setR2q(null);
			qqDetailInfo.setQ2r(null);
			log.error("开启同步错误：", e);
			return false;
		}
		return true;
	}
	
}
